<template>
  <!-- 这是一个登录注册模板 -->
  <!-- 老师账号通过注册后，向管理员申请 -->
  <!-- user表  Id userName password Email Role-->
 
  <!-- 主要内容居中 -->
  <div class="center">
    <LoginBG></LoginBG>
    <h1>Python 在线学习平台</h1>
    <!-- 中间卡牌 -->
    <div class="logon">
      <!-- 左侧 -->
      <div :class="overlaylong">
        <!-- 登录表单 -->
        <div class="loginBox" v-if="disfiex == 0">
          <span class="subTitle">登录</span>
          <el-input placeholder="请输入账号" v-model="login_from.userName"></el-input>
          <!-- 这里有个警告把 type="password"删了就没有了 或者加个form 但是不好看 -->
          <form>
          <el-input placeholder="请输入密码" v-model="login_from.password" type="password"></el-input>
        </form>
          <button class="subButton" @click="onLogin()">登录</button>
          <span class="forget" @click="$router.push({name:'ChangePassword'})">忘记密码？</span>
        </div>
        <!-- 注册表单 -->
        <div class="registerBox" v-if="disfiex == 1">
          <h2 class="subTitle">注册</h2>
          <el-input placeholder="请输入账号" v-model="register_from.userName"></el-input>
          <form>
          <el-input placeholder="请输入密码" type="password" v-model="register_from.password"></el-input>
          </form>
          <el-input placeholder="请输入邮箱" v-model="register_from.email">
            <template #append>
              <span class="code-button" @click="onGetVerifyCode()">获取验证码</span>
            </template>
          </el-input>
          
          <el-input placeholder="请输入验证码" v-model="register_from.code"></el-input>
        
          <button class="subButton" @click="onRegister()">注册</button>
        </div>
      </div>
      <!-- 滑块 -->
      <div :class="overlaytitle">
        <!-- 去注册滑块 -->
        <div class="overlaytitle-Signin" v-if="disfiex == 0">
          <!-- <h2 class="introTitle">Welcome Back!</h2> -->
          <h2 class="introTitle">欢迎回来!</h2>
          <p class="introText">海内存知己，天涯若比邻。</p>
          <div class="cutButton" @click="Signin">去注册</div>
        </div>
        <!-- 去登录滑块 -->
        <div class="overlaytitle-Signup" v-if="disfiex == 1">
          <!-- <h2 class="introTitle">Hello new Friend!</h2> -->
          <h2 class="introTitle">欢迎新朋友!</h2>
          <p class="introText">我欲乘风归去，又恐琼楼玉宇，高处不胜寒。</p>
          <div class="cutButton" @click="Signup">去登录</div>
        </div>
      </div>
    </div>
  </div>

 
</template>
   
<script setup>
import userHttp from "@/api/userHttp";
import { openInfo } from "@/utils/openInfo";
import { ref,reactive } from "vue";
import { useRouter } from "vue-router";
import useUserStore from "@/stores/useUserStore";
import LoginBG from "@/components/LoginBG.vue";
/*数据处理------------------------------------ */
const router = useRouter();
const userStore = useUserStore();
//用户登录表单
const login_from = ref({
  userName: "",
  password: "",
});

//点击登录 --如果浏览器有token，则直接登录-还没有加这个功能
function onLogin() {
  //验证表单完整性
  if (login_from.value.userName == "" || login_from.value.password == "") {
    openInfo("请输入完整表单信息!",'warning');
  } else {
    //发送登录请求
    userHttp.login(login_from.value).then((res) => {
      //如果状态码为200 则登录页面 把用户信息存到store
      if (res.code == 200) {
        console.log(res.data.role=="管理员");
        
        //如果是管理员账号
        if(res.data.role=="管理员"){
          router.push({ name: "AccountMgmt" });
        }else{
          router.push({ name: "Home" }); //跳到首页
        }
        userStore.setUserInfo(res.data);
        openInfo(res.msg,'success');
        
      }else{
        openInfo(res.msg,'warning');
      }
    });
  }
}
//用户注册表单
const register_from = reactive({
  userName: "",
  password: "",
  email: "",
  code: null,
  codeType:"register",//验证码类型此处为注册
})

//获取验证码
function onGetVerifyCode() {
  if (register_from.email == "") {
    openInfo("请先输入邮箱！",'warning')
  } else {
    userHttp.sendVerifyCode(register_from.email,register_from.codeType).then((res) => {
      if (res.code == 200) {
        openInfo(res.msg,'success')
      }else{
        openInfo(res.msg,'warning')
      }
      
    })
  }
}

//添加一个数据验证
//验证用户名是否由11位字母或数字组成
function is11AlphaNumeric(str) {
    // 正则表达式：匹配11位数字或字母
    const regex = /^[a-zA-Z0-9]{11}$/;
    return regex.test(str);
}

//点击注册
function onRegister() {
  // 验证表单完整性
  if (register_from.userName == "" || register_from.password == "" || register_from.email == "" || register_from.code == null) {
    openInfo("请输入完整表单信息!",'warning')
    //如果不是由11位数字或字母组成
  }else if(!is11AlphaNumeric(register_from.userName)){
    openInfo("账号应该由11位字母或数字!")
  }else if(!is11AlphaNumeric(register_from.password)){
    openInfo("密码应该由11位字母或数字!") //密码这个没有测试过
  }else {
    userHttp.register(register_from).then((res) => {
      if (res.code == 200) {
        openInfo(res.msg,"success")
        userStore.setUserInfo(res.data)
        router.push({ name: 'Home' })//跳到列表
      }else{
        openInfo(res.msg,'warning')
      }
    })
  }
}



/*----------------------------------------------- */
// 样式切换
const overlaylong = ref("overlaylong");
const overlaytitle = ref("overlaytitle");
//显示切换 0为登录 1为注册
const disfiex = ref(0);
function Signin() {
  overlaylong.value = "overlaylongleft";
  overlaytitle.value = "overlaytitleright";
  setTimeout(() => {
    disfiex.value = 1;
  }, 200);
}
function Signup() {
  overlaylong.value = "overlaylongright";
  overlaytitle.value = "overlaytitleleft";
  setTimeout(() => {
    disfiex.value = 0;
  }, 200);
}
</script>
  
  <style scoped>
.center {
  width: 100vw;
  height: 100vh;
  background: url("/src/assets/img/11111.png") no-repeat; /*不平铺*/
  background-size: cover; /*覆盖 */
  /* 居中 */
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

/* 中间标题 */
h1 {
  font-size: 30px;
  font-style: italic;
  font-weight: 400;
  color: rgb(200, 206, 233);
  margin-bottom: 50px;

}

/* 中间卡片 */
.logon {
  width: 60%;
  height: 60%;
  border-radius: 20px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22); /*阴影 */
  /* position: relative; */
  overflow: hidden; /*溢出隐藏 */
  max-width: 100%;
  min-width: 800px;
  min-height: 500px;
  display: flex;
}

/* 表单滑块初始 */
.overlaylong {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  backdrop-filter: blur(1px); /*模糊 */
  /*渐变 图片 白色 透明 4选1 */
  /* background: linear-gradient(to right, #669ade, #29eac4); */
  /* background-image: url("src/assets/ys4.jpg") no-repeat; */
  /* background-color: #fff; */
  background-color: rgb(217, 217, 195, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
}

/*左边输入框改变样式从左到右 */
.overlaylongleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  /* background: linear-gradient(to right, #29eac4, #27a71b); */
  backdrop-filter: blur(1px);
  background-color: rgb(217, 217, 195, 0.2);
  /* background-color: #fff; */
  /* 元素在水平方向上向右平移自身宽度的 100% */
  transform: translateX(100%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 从右边到左边后变色 */
.overlaylongright {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  backdrop-filter: blur(1px); /*模糊 */
  background-color: rgb(217, 217, 195, 0.2);
  /* background: linear-gradient(to right, #669ade, #29eac4); */
  /* background-color: #fff; */
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

/*初始右边 */
.overlaytitle {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  /* 给不给背景色都一样 */
  background-color: rgba(208, 33, 33, 0);
  display: flex;
  align-items: center;
  justify-content: center;
}

/*滑块简介标题 */
.introTitle {
  font-size: 35px;
  color: #f5b2b2;
  margin-top: 20px;
}
/*滑块简介内容 */
.introText {
  font-size: 18px;
  color: #fff;
  margin-top: 20px;
  font-family: "宋体";
  font-style: italic;
}

/* 透明左边 */
.overlaytitleleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
}

/* 透明右边 */
.overlaytitleright {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-100%);
  transition: transform 0.6s ease-in-out;
}

/*去注册初始 */
.overlaytitle-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

/* 去登录初始 */
.overlaytitle-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

/*表单标题 */
.subTitle {
  font-size: 28px;
  color: rgb(255, 255, 255);
  font-family: "微软雅黑";
  font-weight: 500;
  letter-spacing: 20px;
}

/*登录表单盒子居中内容部分 */
.loginBox {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

/*注册表单盒子居中内容部分 */
.registerBox {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

/*获取验证码按钮 */
.code-button {
  cursor: pointer; /*触碰变手指 */
  user-select: none; /*无法选中 */
  font-family: "楷体";
  font-size: 16px;
}
/* 验证码按钮 */
.el-input >>> .el-input-group__append:hover {
  background-color: rgb(255, 255, 255);
}

/*输入框 */
.el-input {
  margin-top: 20px;
  height: 50px;
  width: 300px;
}
/* 输入框透明 */
.el-input >>> .el-input__wrapper {
  background-color: transparent !important;
}
/*输入框字体样式 */
.el-input >>> .el-input__wrapper .el-input__inner {
  color: #81e3ee !important;
  font-family: "楷体";
  font-size: 18px;
}

/*忘记密码 */
.forget {
  font-size: 18x;
  margin-top: 20px;
  font-family: "幼圆";
  user-select: none; /*禁止选中*/
  cursor: pointer; /*触摸变手*/
}
.forget:hover {
  color: #a7f1f9;
}

/*切换按钮 */
.cutButton {
  width: 180px;
  height: 40px;
  border-radius: 5px;
  border: 1px solid #eee3e3;
  color: #fff;
  font-size: 15px;
  text-align: center;
  line-height: 40px;
  margin-top: 40px;
  cursor: pointer; /*触碰变手指 */
  user-select: none; /*无法选中 */
}
.cutButton:hover {
  color: #a7f1f9;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25); /*阴影 */
}

/* 提交按钮 */
.subButton {
  width: 180px;
  height: 40px;
  border-radius: 5px;
  background-color: rgba(43, 170, 149, 0.2);
  border: none;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-top: 30px;
  cursor: pointer; /*触碰变手指 */
  transition: background-color 0.8s ease, box-shadow 2s ease; /* 添加过渡
  user-select: none; /*无法选中 */
}

.subButton:hover {
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25); /*阴影 */
  background-color: rgba(43, 170, 149,0.8);
}
</style>